// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

import { _decorator, LabelComponent, Component } from "cc";
const { ccclass, property, requireComponent } = _decorator;

@ccclass("updateValueLabel")
@requireComponent(LabelComponent)
export class updateValueLabel extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;
    isPlaying: boolean = false;
    startVal: number = 0;
    endVal: number = 0;
    diffVal: number = 0;
    currTime: number = 0;
    changingTime: number = 0;
    label: LabelComponent = null;

    start () {
        // Your initialization goes here.
        
    }

    playUpdateValue(startVal, endVal, changingTime) {
        this.startVal = startVal;
        this.endVal = endVal;

        this.diffVal = this.endVal - this.startVal;
        
        this.currTime = 0;
        this.changingTime = changingTime;
        
        this.label = this.node.getComponent(LabelComponent);
        this.label.string = startVal.toString();

        this.isPlaying = true;
    }

    update(dt) {
        if(!this.isPlaying) {
            return;
        }

        if(this.currTime < this.changingTime) {
            this.currTime += dt;
            
            var currVal = this.startVal + parseInt((this.currTime / this.changingTime * this.diffVal).toString());
            if (currVal < this.startVal) {
                currVal = this.startVal;
            } else if (currVal > this.endVal) {
                currVal = this.endVal;
            }
            
            this.label.string = `${currVal}`;
            return;
        }

        this.label.string = `${this.endVal}`;
        this.isPlaying = false;
        
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
